package inglar.autotc.client;

import inglar.autotc.client.resource.ResourceHandler;
import inglar.autotc.client.ui.DialogBoxLogin;
import inglar.autotc.shared.db.Actions;
import inglar.autotc.shared.db.Templates;

import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

/**
 * Provides convenience functions.
 * @author Lee
 */
public class Utilities {
	
	public static boolean isDebugMode = true;
	
	private Utilities() {
		/*
		 * DO NOT INSTANTIATE THIS CLASS.
		 */
	}
	
	/**************************************************************************/
	
	public static HostServiceAsync rpc = HostService.Util.getInstance();
	
	public static void getTemplateList(AsyncCallback<Templates[]> callback) {
		rpc.getTemplateList(getUserID(), callback);
	}
	
	public static void setAction(Actions action, AsyncCallback<Void> callback) {
		rpc.setAction(getUserID(), action, callback);
	}
	
	/**************************************************************************/
	
	public static String getUserID() {
		return Cookies.getCookie("QID");
	}
	
	public static void setUserID(String value) {
		Cookies.setCookie("QID", value);
	}
	
	public static String getUserName() {
		return Cookies.getCookie("QN");
	}
	
	public static void setUserName(String value) {
		Cookies.setCookie("QN", value);
	}
	
	private static DialogBox dialogBoxPleaseWait = null;
	
	public static void showLoadingModal() {
		if(dialogBoxPleaseWait == null) {
			dialogBoxPleaseWait = new DialogBox(false, true);
			dialogBoxPleaseWait.setAnimationEnabled(true);
			dialogBoxPleaseWait.setGlassEnabled(true);
			dialogBoxPleaseWait.setHTML("");
			HorizontalPanel panel1 = new HorizontalPanel();
			dialogBoxPleaseWait.setWidget(panel1);
			Label lblPleaseWait = new Label("Please wait...");
			panel1.add(lblPleaseWait);
			lblPleaseWait.getElement().setAttribute("style", "margin-right: 7px;");
			Image image1 = new Image(ResourceHandler.INSTANCE.fbLoadBlue());
			panel1.add(image1);
		}
		dialogBoxPleaseWait.center();
	}
	
	public static void hideLoadingModal() {
		if(dialogBoxPleaseWait == null) return;
		dialogBoxPleaseWait.hide();
	}
	
	public static void failureAlert(String tag, String description, Throwable caught) {
		if(caught == null) return;
		String msg = "An error occurred. Please reload the page after a few moments.";
		if(isDebugMode) {
			msg += "\n\nerror: ";
			if(description == null || description.isEmpty()) msg += "(unspecified error description)\n";
			else msg += description + "\n";
			if(tag == null || tag.isEmpty()) msg += "(unspecified error context)\n";
			else msg += "at " + tag + "\n";
			msg += "caught instance: " + caught.getClass().getName() + "\n";
			msg += ((caught.getMessage() == null)? "{null}" : "\"" + caught.getMessage() + "\"");
		}
		Window.alert(msg); 
	}
	
	public static String tabchar = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; // 8 spaces = 1 tabchar
	
	public static String whiteSpace(String html) {
		return html.replaceAll(" ", "&nbsp;").replaceAll("\n", "<br/>").replaceAll("\t", tabchar);
	}
	
	public static String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
	}

	public static boolean isLogged() {
		String username = Cookies.getCookie("QN");
		String userid = Cookies.getCookie("QID");
		if(username != null && userid != null) return true;
		return false;
	}

	public static void logout() {
		Cookies.removeCookie("QID");
		Cookies.removeCookie("QN");
		Cookies.removeCookie("QP");
		Location.reload();
	}

	public static void login(final String username, String password, final DialogBoxLogin ldb) {
		
		AsyncCallback<String> loginCallback = new AsyncCallback<String>() {			
			@Override
			public void onSuccess(String result) {
				if(result == null || result.startsWith("ERROR")) {
					ldb.reset();
					Window.alert("Unknown username or password.");
				} else {
					Cookies.setCookie("QID", result.trim(), null);
					Cookies.setCookie("QN", username.trim(), null);
					Cookies.setCookie("QP", "0");
					Location.reload();
				}
			}
			@Override
			public void onFailure(Throwable caught) {
				failureAlert("loginCallback", "RPC failure", caught);
				ldb.reset();
			}
		};
	
		rpc.login(username, password, loginCallback);
	}
	
	
}
